package library.library.dao.impl;


import library.library.dao.AdministratorsDao;
import library.library.model.Administrators;
import org.hibernate.SessionFactory;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import util.page1.PageUtils;
import util.project.dao.DaoImpl;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * Created by MondaDev01 on 2017/11/2.
 */
@Repository
public class AdministratorsDaoImpl extends DaoImpl implements AdministratorsDao {

    @Resource
    private JdbcTemplate jdbcTemplate;

    @Resource(name = "sessionFactory")
    public void setSessionFactoryOverride(SessionFactory sessionFactory)
    {
        super.setSessionFactory(sessionFactory);

    }



    public JdbcTemplate getJdbcTemplate() {
        return jdbcTemplate;
    }

    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }


    public int AddAdministrators(String name, String password, Integer sex) {
        String sql = "INSERT INTO `administrators`(`name`,`password`,`sex`,`create_date`) VALUES(?,?,?,?) ";
        int i = this.getJdbcTemplate().update(sql,new Object[]{name,password,sex,new Date()});
        return i;
    }

    public PageUtils<Administrators> page(String name, Integer sex, Date qDate, Date hDate, Integer pageIndex, Integer pageSize) {
        PageUtils<Administrators> pageModel = new PageUtils<Administrators>(pageIndex, pageSize);
        StringBuilder sql = new StringBuilder("select * from `administrators` where 1=1");
        StringBuilder count = new StringBuilder("select count(*) from `administrators` where 1=1");
        if (name != null ){
            sql.append(" and name like '%" + name + "%' ");
            count.append(" and name like '%" + name + "%' ");
        }
        if (sex != null){
            sql.append(" and sex = " + sex );
            count.append(" and sex = " + sex );
        }

        if (qDate != null){
            sql.append(" and qDate = '" + qDate + "'");
            count.append(" and qDate = '" + qDate + "'");
        }

        if (hDate != null){
            sql.append(" and qDate = '" + hDate + "'");
            count.append(" and qDate = '" + hDate + "'");
        }

        int ye = 0;
        if (pageIndex != null){
            ye = (pageIndex-1) * pageSize;
        }

        sql.append(" order by create_date desc limit "+ ye + "," + pageSize);

        System.out.println(sql);
        System.out.println(count);

        List<Administrators> administratorsList  = getJdbcTemplate().query(sql.toString(), new BeanPropertyRowMapper(Administrators.class));

        Integer adminCount  = getJdbcTemplate().queryForObject(count.toString(),Integer.class);

        pageModel.setDataList(administratorsList);
        pageModel.setTotalCount(adminCount);
        return pageModel;
    }

    public Integer add(Administrators administrators) {
        /*return  this.session.save(administrators);*/
        return (Integer) this.getSession().save(administrators);
    }

    public Administrators get(Integer id) {
        return (Administrators) this.session.get(Administrators.class,id);
    }


}
